package com.suntop.carousel.qlmediaplay.tools;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.os.StatFs;
import android.text.TextUtils;


import com.suntop.carousel.qlmediaplay.bean.FileOption;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Locale;
@SuppressLint("NewApi")
public class FileUnit {
    /**
     * 获取可用的空间大小 单位字节
     *
     * @param file
     * @return
     */
    public static long freeSpace(File file) {
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }
        StatFs fileStats = new StatFs(file.getAbsolutePath());
        long size = fileStats.getAvailableBlocksLong() * fileStats.getBlockSizeLong();
        return size;
    }

    /**
     * 获取后缀
     *
     * @param filePath
     * @return
     */
    public static String getSuffix(String filePath) {
        if (TextUtils.isEmpty(filePath)) {
            return "";
        }
        if (filePath.endsWith(".")) {
            return "";
        }
        int index = filePath.lastIndexOf(".");
        if (index != -1) {
            return filePath.substring(index + 1).toLowerCase(Locale.US);
        } else {
            return "";
        }
    }


    /**
     * 获取文件名称
     * @param url
     * @return
     */
    public static String takeFileName(String url){
        if (TextUtils.isEmpty(url)){
            return "";
        }
        String[] split = url.split("/");
        if (split.length==0){
            return "没有文件名";
        }
        String name = split[split.length - 1];
        String suffix = getSuffix(name);
        if (TextUtils.isEmpty(suffix)){
            return "未知类型";
        }
        return name;
    }
    /**
     * 删除文件夹下的文件
     * 不删除文件夹
     *
     * @param path
     */
    public static void deleteFile(String path) {
        if (!TextUtils.isEmpty(path)) {
            File file = new File(path);
            if (file.isDirectory()) {
                File[] files = file.listFiles();
                if (files != null && files.length > 0) {
                    for (File file1 : files) {
                        deleteFile(file1.getAbsolutePath());
                    }
                }
            } else {
                boolean delete = file.delete();
            }
        }
    }

    /**
     * 存储矢量位图 无压缩
     *
     * @param bitmap
     * @param name
     * @param path
     */
    public static File saveBitmapFile(Bitmap bitmap, String name, String path) throws IOException {
        if (bitmap.isRecycled()){
            return null;
        }
        File file = new File(path);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }
        File rootFile = new File(file, name);
        if (rootFile.exists()) {
            if (rootFile.length() < 10) {
                boolean delete = rootFile.delete();
            } else {
                return rootFile;
            }
        }
        if (!rootFile.exists()) {
            boolean newFile = rootFile.createNewFile();
        }
        try {
            //文件输出流
            FileOutputStream out = new FileOutputStream(rootFile);
            //bitmp压缩到本地，原图就100
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (!bitmap.isRecycled())
                bitmap.recycle();
        }
        return rootFile;
    }

    /**
     * 存储BITMAP
     * @param fileOption
     * @return
     * @throws IOException
     */
    public static File saveBitmapFile(FileOption fileOption) throws IOException {
        String filePath = fileOption.getFilePath();
        if (TextUtils.isEmpty(filePath)) {
            return null;
        }
        File file = new File(filePath);
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }
        String fileName = fileOption.getFileName();
        if (TextUtils.isEmpty(fileName)) {
            fileName = System.currentTimeMillis() + ".jpg";
        }
        File newFile = new File(file, fileName);
        if (newFile.exists()) {
            if (newFile.length() > 20) {
                return newFile;
            }
            boolean delete = newFile.delete();
        }
        Bitmap newBitmap = null;
        try {
            //文件输出流
            FileOutputStream out = new FileOutputStream(newFile);
            //bitmp压缩到本地，原图就100
            newBitmap = BitmapTools.comp(fileOption.getBitmap(), fileOption.getWith(),
                    fileOption.getHeight(), fileOption.getQuality());
            newBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
            out.flush();
            out.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            if (!fileOption.getBitmap().isRecycled())
                fileOption.getBitmap().recycle();
            if (newBitmap != null && !newBitmap.isRecycled())
                newBitmap.recycle();
        }
        return newFile;
    }


    /**
     * 根据文件名查找文件
     *
     * @param file
     * @param fileName
     * @return
     */
    public static File queryFileByName(File file, String fileName) {
        File newFile = new File(file, fileName);
        if (newFile.isFile() && newFile.exists() && !newFile.getPath().contains("LOST.DIR")) {
            return newFile;
        }
        File[] files = file.listFiles();
        if (files != null) {
            for (File fileChild : files) {
                if (!fileChild.getPath().contains("LOST.DIR") && fileChild.isDirectory())
                    newFile = queryFileByName(fileChild, fileName);
                if (newFile != null&&newFile.exists()) {
                    break;
                }

            }
        }
        return newFile;
    }
}
